package com.dhcc.android.base.utils.datamanager.bussiness;

import com.dhcc.android.base.utils.datamanager.bussiness.event.SelectedIndexChangedManager;

import org.dom4j.DocumentException;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**该类负责工程文件的管理
 * 主要管理工作空间内的工程文件
 * @author lzy
 *
 */
public class GlobalTaskCollection {

	private static List<WholeTask> pWholeTaskList = new ArrayList<WholeTask>();
	public static int pSelectedWholeIndex = -1;
	/**当前查看的调查任务
	 * 
	 */
	private static int mCurrentCheckWholeIndex = -1;
	public static SelectedIndexChangedManager SelectedIndexChanged=new SelectedIndexChangedManager();

	public static List<WholeTask> getWholeTaskList(){
		return pWholeTaskList; 
	}

	/**设置正在查看的调查任务
	 * @param index 调查任务序列号
	 * @return
	 */
	public static WholeTask setCurrentWholeTaskByIndex(int index){
		if (index >= 0 && index < pWholeTaskList.size()){
			mCurrentCheckWholeIndex = index;
			return pWholeTaskList.get(mCurrentCheckWholeIndex);
		}
		return null;
	}

	/**获取正在查看的调查任务
	 * @return
	 */
	public static WholeTask getCurrentCheckWholeTask(){
		if (mCurrentCheckWholeIndex >= 0 && mCurrentCheckWholeIndex < pWholeTaskList.size()){
			return pWholeTaskList.get(mCurrentCheckWholeIndex);
		}
		return null;		
	}

	/**获取指定序号的任务
	 * @param index 指定任务在所有任务中德分组
	 * @return
	 */
	public static WholeTask getWholeTaskByIndex(int index){
		if (index >= 0 && index < pWholeTaskList.size()){
			return pWholeTaskList.get(index);
		}
		return null;
	}

	public static int getSelectedWholeIndex(){
		return pSelectedWholeIndex; 
	}

	/**设置选中项目为当前正在查看的项目
	 * @throws Exception
	 */
	public static void SetCheck2Selected()throws Exception{
		if (pSelectedWholeIndex != mCurrentCheckWholeIndex){
			//			DisposeTaskLayer(pSelectedWholeIndex);
			pSelectedWholeIndex = mCurrentCheckWholeIndex;

			if (SelectedIndexChanged != null){
				SelectedIndexChanged.fireListener();
			}
		}
	}

	public static void setSelectedWholeIndex(int value) throws Exception{
		//		if (pSelectedWholeIndex != value){
		//			DisposeTaskLayer(pSelectedWholeIndex);
		pSelectedWholeIndex = value;

		if (SelectedIndexChanged != null){
			SelectedIndexChanged.fireListener();
		}
		//		}
	}

	public static WholeTask getSelectedWholeTask(){
		if (pSelectedWholeIndex >= 0 && pSelectedWholeIndex < pWholeTaskList.size()){
			return pWholeTaskList.get(pSelectedWholeIndex);
		}
		return null;
	}

	public static void DisposeTaskLayer(int i){
		if (i < 0 || i >= pWholeTaskList.size())
			return;
		pWholeTaskList.get(i).DisposeLayer();
	}

	/**添加任务配置信息
	 * @param filePath
	 */
	public static void AddWholeConfig(String filePath){
		File file=new File(filePath);

		if (file.exists() && filePath.substring(filePath.indexOf(".")).equalsIgnoreCase(".tcf")){
			WholeTask task = new WholeTask();
			try {
				task.LoadFromFile(filePath);
				pWholeTaskList.add(task);
				if (pSelectedWholeIndex == -1)
					pSelectedWholeIndex = 0;

			} catch (DocumentException e) {
				e.printStackTrace();
			}
		}
	}


	/**添加任务配置信息
	 * @param filePath
	 */
	public static void AddWholeTask(String filePath){
		File file=new File(filePath);

		if (file.exists() && filePath.substring(filePath.indexOf(".")).equalsIgnoreCase(".TCF")){
			WholeTask task = new WholeTask();
			try {
				task.LoadFromFile(filePath);
				pWholeTaskList.add(task);
				if (pSelectedWholeIndex == -1)
					pSelectedWholeIndex = 0;

			} catch (DocumentException e) {
				e.printStackTrace();
			}
		}
	}

	public static void ClearWholeTask(){
		pWholeTaskList.clear();   
	}

}

